package com.dcdata.jt809server.head.impl;

import com.dcdata.jt809server.common.consts.LoginResponseCode;
import com.dcdata.jt809server.common.consts.UserInfo;
import com.dcdata.jt809server.packet.TrafficLoginRequestPacket;
import com.dcdata.jt809server.packet.TrafficLoginResponsePacket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * program: JT809Server
 * description: 登录适配
 * @author DCDATA
 * @date 2020-03-13 09:09
 **/
@Slf4j
public class TrafficLoginHandler extends SimpleChannelInboundHandler<TrafficLoginRequestPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TrafficLoginRequestPacket msg) {
        log.info("登录报文{}", msg);
        TrafficLoginResponsePacket trafficLoginResponsePacket = new TrafficLoginResponsePacket();
        byte loginResponseCode = valid(msg);
        trafficLoginResponsePacket.setResul(loginResponseCode);
        trafficLoginResponsePacket.setVerifyCode(0);
        // 登录响应
        log.info("登录响应：{}", trafficLoginResponsePacket);
        ctx.channel().writeAndFlush(trafficLoginResponsePacket);
    }

    /**
     * 用户名密码校验
     */
    private byte valid(TrafficLoginRequestPacket msg) {
        int userId = msg.getUserId();
        String password = msg.getPassword();
        log.info("接收到了登录的请求->用户名：{};密码：{}", userId, password);

        /*if (UserInfo.USER_ID != userId) {
            log.info("USER_ID不正确");
            return LoginResponseCode.USERNAME_ERROR;
        }
        if (!UserInfo.PASSWORD.equals(password)) {
            log.info("PASSWORD_ERROR");
            return LoginResponseCode.PASSWORD_ERROR;
        }*/

        log.info("登录验证成功");
        return LoginResponseCode.SUCCESS;
    }
}
